import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '@renderer/views/login/login.vue'
import Home from '@renderer/views/home/index.vue'

const routes = [
    // 精确匹配 #/login，指向Login页面
    { path: '/login', component: Login, exact: true },
    // 精确匹配 #/home，指向Home页面
    { path: '/home', component: Home, exact: true },
    // 空hash，则跳转至Login页面
    { path: '', redirect: 'login' },
    // 未匹配，则跳转至Login页面
    { path: '/:pathMatch(.*)', redirect: 'login' },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})
router.beforeEach((to, from, next) => {
    //不需要登录的页面
    const passPath = ['/login']
    if (passPath.includes(to.path)) {
      next()
    } else {
      if (localStorage.getItem('token')) {
        if(to.path==from.path && from.path!='/') return
        // 到如果匹配到路由结果
        if (to.matched.length > 0) {
          next()
        }
      } else {
        next('/login')
      }
    }
  })
export default router
